package com.gpjcjl.ui;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.base.lib.Logs;
import com.gpjcjl.App;
import com.gpjcjl.entity.Result;
import com.gpjcjl.http.HttpManager;
import com.gpjcjl.http.HttpService;
import com.gpjcjl.http.RxSchedulers;
import com.gpjcjl.http.SingleLiveEvent;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author yinbiao
 */
public class PropertyViewModel extends AndroidViewModel {

    private static final String TAG = "PropertyViewModel";

    CompositeDisposable disposable;

    public PropertyViewModel(@NonNull Application application) {
        super(application);
        disposable = new CompositeDisposable();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        //页面销毁时调用
        Logs.d(TAG, "onCleared:");
        disposable.clear();
    }

    /**
     * 编辑产权单位
     *
     * @return LiveData
     */
    public LiveData<Result<String>> editProperty(String name, String id) {
        MutableLiveData<Result<String>> result = new SingleLiveEvent<>();
        disposable.add(HttpManager.getInstance().getService(HttpService.class)
                .editProperty(App.getInstance().getAndroidId(), name, id)
                .compose(RxSchedulers.ioMain())
                .subscribe(bean -> {
                    result.setValue(bean);
                }, throwable -> {
                    result.setValue(Result.error(throwable.getMessage()));
                    Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                }));
        return result;
    }


}
